// pages/search/search.js
const db = wx.cloud.database(); //初始化数据库
Page({

  /**
   * 页面的初始数据
   */
  data: {
    search: '',
    thins: [],
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
/*     wx.showLoading({
      title: '加载中...',
    })
    const that = this
    db.collection("ershou").orderBy('createTime','desc').get()
      .then(res => {
        console.log("全部数据为：", res)
        that.setData({
          thins: res.data,
        })
      })
    setTimeout(() => {
      wx.hideLoading()
    }, 800) */
  },

    // 跳转到详情 页面
    to(res) {
      const index = res.currentTarget.dataset.index
      const _id = this.data.thins[index]._id
      const imgdata = this.data.thins[index]
   
      wx.setStorageSync('ershouID', _id)
      wx.cloud.callFunction({
        name: "look",
        data: {
          _id,
          lookNumber: this.data.thins[index].lookNumber,
        },
        success: (res) => {
          //this.onLoad()
        },
        fail: (res) => {
          showToast({
            icon: 'none',
            title: '服务器出错，请重试...'
          })
        }
      })
  
      const dataobj = {
        lookNumber: imgdata.lookNumber,
        beizhu: imgdata.beizhu,
        createTime: imgdata.createTime,
        imgs: imgdata.imgs,
        phonenumber: imgdata.phonenumber,
        newold: imgdata.newold,
        sell: imgdata.sell,
        state: imgdata.state,
        time: imgdata.time,
        title: imgdata.title,
        two: imgdata.two,
        userInfo: imgdata.userInfo,
        type: imgdata.type,
        _id: imgdata._id,
        _openid: imgdata._openid,
      }
      //跳转
      wx.navigateTo({
        url: '/pages/detailsS/detailsS',
        success: (res) => {
          res.eventChannel.emit('acceptDataFromOpenerPage', {
            data: dataobj
          })
        }
      })
  
  
    },

      //  获取输入框值
  searchtext(e) {
    console.log('搜索关键字',e.detail.value)
    this.setData({
      search: e.detail.value,
    })
  },
   
  // 搜索 模糊查询 
  getsearch() {

    let key = this.data.search;
    if( key === '' || key === null) {
      wx.showToast({
        title: '输入不能为空噢',
        icon: "none",
        duration: 1000
      })
       
      return;
    }
    wx.showLoading({
      title: '加载中...',
    })
    console.log("查询的关键字为：", key)
    const db = wx.cloud.database();
    const _ = db.command
    db.collection('ershou').where(_.or([{
      title: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
      {
        beizhu: db.RegExp({
          regexp: '.*' + key,
          options: 'i',
        })
      },
    ])).get({
      success: res => {
        console.log('查询出来的数据为：',res.data)
        this.setData({
          thins: res.data,
          b: 1,
        })
        if(res.data.length === 0 || res.data === null) {
          this.setData({
            b: 0
          })
        }
      },
      fail: err => {
        console.log(err)
      }
    })
    wx.hideLoading()
  },





  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showToast({
      title: '刷新中.....',
      icon:"none"
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showToast({
      title: '没有啦~~',
      icon:"none"
    })

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})